package com.demo.controller;

import com.demo.common.CreateDate;
import com.demo.common.CreateUUID;
import com.demo.common.ShaEncrypt;
import com.demo.common.StringUtil;
import com.demo.model.*;
import com.demo.service.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@Log4j2
public class AppIndexController {

    @Resource
    private AppTabUsersService users_service;
    @Resource
    private TabUsersShopsService us_service;
    @Resource
    private TabUsersAccHistoryService his_service;

    @Resource
    private TabUsersAccService acc_service;

    @Resource
    private TabMoneyShopsService money_service;

    @RequestMapping(value = "app_login")
    public String app_login(){
        return "app/login";
    }

    @RequestMapping(value = "app_login_check")
    public String app_login_check(String loginname, String password, HttpServletRequest request){
        String msg = "";
        TabUsers users = new TabUsers();
        users.setLoginname(loginname);
        users.setPassword(ShaEncrypt.shaEncode(password));
        List<TabUsers> check_login_list = users_service.select_app_check_login(users);
        if(check_login_list.isEmpty()){
            //登录失败
            msg = "error";
        }else{
            //登录成功
            msg = "success";
            String user_loginname = "";
            String id = "";
            for(TabUsers u:check_login_list){
                user_loginname = u.getLoginname();
                id = u.getId();
            }
            request.getSession().setAttribute("user_loginname",user_loginname);
            request.getSession().setAttribute("user_id",id);
            request.getSession().setMaxInactiveInterval(18200);
        }
        request.setAttribute("msg",msg);
        return "app/login";
    }

    @RequestMapping(value = "pres_users")
    public String pres_users(){
        return "app/pres_users";
    }

    @RequestMapping(value = "pres_users_save")
    public String pres_users_save(TabUsers users, MultipartFile file, HttpServletRequest request) throws IOException {
        List<TabUsers> check_loginname_list = users_service.findByloginname(users.getLoginname());
        if(check_loginname_list.isEmpty()){

            String path=request.getServletContext().getRealPath("/upload");
            File dir=new File(path);
            dir.mkdirs();
            String srcName=file.getOriginalFilename();
            File dest=new File(dir,srcName);
            file.transferTo(dest);


            String id = CreateUUID.getuuid();
            String source = "前台";
            String datetimes = CreateDate.getDate();
            users.setPassword(ShaEncrypt.shaEncode(users.getPassword()));
            users.setId(id);
            users.setFlag("1");
            users.setEm1(source);
            users.setEm2("");
            users.setEm3(datetimes);
            users.setEm4(srcName);
            users.setEm5("");
            boolean b = users_service.save(users);
            request.setAttribute("msg",b);
        }else{
            request.setAttribute("msg",false);
        }
        return "app/pres_users";
    }

    @RequestMapping(value = "MyApp")
    public String MyApp(HttpServletRequest request){
        String user_loginname = (String) request.getSession().getAttribute("user_loginname");
        if(null==user_loginname){
            return "app/login";
        }else{
            List<TabUsers> list = users_service.findByloginname(user_loginname);
            request.setAttribute("list",list);
            return "app/my_app";
        }


    }


    @RequestMapping(value = "footer")
    public String footer(){
        return "app/footer";
    }

    @RequestMapping(value = "money_shop_list")
    public String money_shop_list(String msg,HttpServletRequest request){

        String user_id = (String)request.getSession().getAttribute("user_id");

        String path = request.getContextPath();
        StringBuffer sbf = new StringBuffer();
        List<TabMoneyShops> list = users_service.select_money_shop_list();
        sbf.append("<dl>");
        int num=acc_service.select_if_has_card(user_id);
                for(TabMoneyShops s:list) {
                    TabUsersShops us = new TabUsersShops();
                    us.setUserid(user_id);
                    us.setShopid(s.getId());
                    List<TabUsersShops> check_shop_list = us_service.select_check_us(us);

                    sbf.append("<dd style=\"width: 45%;height:70%;\" >");
                    sbf.append("<a >");
                    sbf.append("<a href=\"do_discription?id="+s.getId()+"\" >");
                    sbf.append("<img  src=\"" + path + "/upload/" + s.getPic() + "\" onerror=\"this.src='" + path + "/upload/shop.jpg'\">");
                    sbf.append("</a >");
                    sbf.append("<div class=\"wz\">");
                    sbf.append("<a href=\"do_discription?id="+s.getId()+"\">");
                    sbf.append("<h2 >" + s.getMsname() + "</h2>");
                    sbf.append("</a>");
                    sbf.append("<p>单价:<span>" + s.getMoney() + "</span> ");
                    if (s.getIsv().equals("1")) {
                        sbf.append("<div style='background-color:red;font-size:12px;color:white;'>【精品】</div>");
                    }
                    if (s.getIsv().equals("0")) {
                        sbf.append("<div style='background-color:white;font-size:12px;color:white;'>【精品】</div>");
                    }
                    if (check_shop_list.isEmpty()) {
                        sbf.append("<input type=\"button\" value=\"直接购买\" onclick=\"doshop('" + s.getId() + "','"+num+"','" + s.getMoney() + "')\"/>");
                    } else {
                        sbf.append("<input type=\"button\" value=\"已购买\" />");
                    }

                    sbf.append("</p>");
                    sbf.append("</div>");
                    sbf.append("</a>");
                    sbf.append("</dd>");
                }
        sbf.append("</dl>");




        request.setAttribute("table",sbf);
        request.setAttribute("msg",msg);
        return "app/money_shop_list";



    }

    @RequestMapping(value = "do_discription")
    public String do_discription(String id,HttpServletRequest request){
        TabMoneyShops shop=money_service.findById(id);
        int same_shop_users_num=us_service.same_shop_users(id);
        TabUsersShops sh=new TabUsersShops();
        sh.setShopid(id);
        String user_id=(String) request.getSession().getAttribute("user_id");
        sh.setUserid(user_id);
        List<TabUsers> list=us_service.same_shop_users_list(sh);
        request.setAttribute("shop",shop);
        request.setAttribute("same_shop_users_num",same_shop_users_num);
        request.setAttribute("list",list);
        return "app/description";
    }

    @RequestMapping(value = "money_shop_buy_shops")
    public String money_shop_buy_shops(String shopid,String money,HttpServletRequest request){
        String user_id = (String)request.getSession().getAttribute("user_id");
        String msg = "";
        if(StringUtil.isEmpty(user_id)){
            return "app/login";
        }else{
            TabUsersAcc history = his_service.findByuserid(user_id);

            String my_money = history.getMonnum();


                double _my_money = Double.parseDouble(my_money);//用户钱包中的钱数
                double _money = Double.parseDouble(money);//当前购买商品的钱数

                if (_my_money < _money) {
                    msg = "error";
                } else {

                    String id = CreateUUID.getuuid();
                    String datetimes = CreateDate.getDateTimes();
                    TabUsersShops s = new TabUsersShops();
                    s.setId(id);
                    s.setShopid(shopid);
                    s.setUserid(user_id);
                    us_service.save(s);
                    String idd = CreateUUID.getuuid();
                    TabUsersAccHistory his = new TabUsersAccHistory();
                    his.setId(idd);
                    his.setUserid(user_id);
                    his.setAccid(history.getId());
                    his.setMonnum("-" + money);
                    his.setWay("购买");
                    his.setWaytimes(datetimes);
                    his.setEm1("");
                    his.setEm2("");
                    his.setEm3("");
                    his.setEm4("");
                    his.setEm5("");
                    his_service.save(his);

                    double all_money = _my_money - _money;

                    TabUsersAcc accc = new TabUsersAcc();
                    accc.setId(history.getId());
                    accc.setUserid(user_id);
                    accc.setMonnum(all_money + "");
                    acc_service.up_money(accc);
                    msg = "success";

                }
            return "redirect:money_shop_list?msg="+msg;



        }

    }



    @RequestMapping(value = "money_shop_isv_list")
    public String money_shop_isv_list(String msg,HttpServletRequest request){

        String user_id = (String)request.getSession().getAttribute("user_id");

        String path = request.getContextPath();
        StringBuffer sbf = new StringBuffer();
        List<TabMoneyShops> list = users_service.select_money_shop_isv_list();
        sbf.append("<dl>");
        int num=acc_service.select_if_has_card(user_id);

        for(TabMoneyShops s:list){

            TabUsersShops us = new TabUsersShops();
            us.setUserid(user_id);
            us.setShopid(s.getId());
            List<TabUsersShops> check_shop_list = us_service.select_check_us(us);

            sbf.append("<dd>");
            sbf.append("<a >");
            sbf.append("<a href=\"do_discription?id="+s.getId()+"\" >");
            sbf.append("<img  src=\"" + path + "/upload/" + s.getPic() + "\" onerror=\"this.src='" + path + "/upload/shop.jpg'\">");
            sbf.append("</a >");
            sbf.append("<div class=\"wz\">");
            sbf.append("<a href=\"do_discription?id="+s.getId()+"\">");
            sbf.append("<h2 >" + s.getMsname() + "</h2>");
            sbf.append("</a>");
            sbf.append("<p>单价:<span>"+s.getMoney()+"</span> ");
            sbf.append("<div style='background-color:red;font-size:12px;color:white;'>【精品】</div>");

            if(check_shop_list.isEmpty()){
                sbf.append("<input type=\"button\" value=\"直接购买\" onclick=\"doshop('"+s.getId()+"','"+num+"','"+s.getMoney()+"')\"/>");
            }else{
                sbf.append("<input type=\"button\" value=\"已购买\" />");
            }

            sbf.append("</p>");
            sbf.append("</div>");
            sbf.append("</a>");
            sbf.append("</dd>");

        }
        sbf.append("</dl>");
        request.setAttribute("table",sbf);
        request.setAttribute("msg",msg);
        return "app/money_shop_isv_list";

    }

    @RequestMapping(value = "money_shop_buy_isv_shops")
    public String money_shop_buy_isv_shops(String shopid,String money,HttpServletRequest request){
        String user_id = (String)request.getSession().getAttribute("user_id");
        String msg = "";
        if(StringUtil.isEmpty(user_id)){
            return "app/login";
        }else{
            TabUsersAcc history = his_service.findByuserid(user_id);

            String my_money = history.getMonnum();

            double _my_money = Double.parseDouble(my_money);//用户钱包中的钱数
            double _money = Double.parseDouble(money);//当前购买商品的钱数

            if(_my_money<_money){
                msg = "error";
            }else {

                String id = CreateUUID.getuuid();
                String datetimes = CreateDate.getDateTimes();
                TabUsersShops s = new TabUsersShops();
                s.setId(id);
                s.setShopid(shopid);
                s.setUserid(user_id);
                us_service.save(s);
                String idd = CreateUUID.getuuid();
                TabUsersAccHistory his = new TabUsersAccHistory();
                his.setId(idd);
                his.setUserid(user_id);
                his.setAccid(history.getId());
                his.setMonnum("-"+money);
                his.setWay("购买");
                his.setWaytimes(datetimes);
                his.setEm1("");
                his.setEm2("");
                his.setEm3("");
                his.setEm4("");
                his.setEm5("");
                his_service.save(his);

                double all_money = _my_money-_money;

                TabUsersAcc accc = new TabUsersAcc();
                accc.setId(history.getId());
                accc.setUserid(user_id);
                accc.setMonnum(all_money+"");
                acc_service.up_money(accc);
                msg = "success";
            }
            return "redirect:money_shop_isv_list?msg="+msg;
        }

    }


    @RequestMapping(value = "center_grzl")
    public String center_grxx(String id,HttpServletRequest request){

        TabUsers t = users_service.findByid(id);
        request.setAttribute("t",t);
        return "app/grxx/center_grzl";
    }

    @RequestMapping(value = "center_grzl_update")
    public String center_grzl_update(TabUsers t,HttpServletRequest request){
        boolean b = users_service.update(t);
        request.setAttribute("b",b);
        return "app/grxx/center_grzl";
    }

    @RequestMapping(value = "center_mmxg")
    public String center_mmxg(HttpServletRequest request){
        String loginname = (String)request.getSession().getAttribute("user_loginname");
        if(StringUtil.isEmpty(loginname)){
            return "app/login";
        }else{
            return "app/grxx/center_mmxg";
        }
    }

    @RequestMapping(value = "center_mmxg_update")
    public String center_mmxg_update(String oldpwd,String newpwd,HttpServletRequest request){
        String loginname = (String)request.getSession().getAttribute("user_loginname");
        if(StringUtil.isEmpty(loginname)){
            return "app/login";
        }else {
            TabUsers user1 = new TabUsers();
            user1.setLoginname(loginname);
            user1.setPassword(ShaEncrypt.shaEncode(oldpwd));
            List<TabUsers> check_list_login = users_service.select_app_check_login(user1);
            if(check_list_login.isEmpty()){
                request.setAttribute("b", "oldpwd_error");
                return "app/grxx/center_mmxg";
            }else{
                TabUsers user2 = new TabUsers();
                user2.setLoginname(loginname);
                user2.setPassword(ShaEncrypt.shaEncode(newpwd));
                boolean b = users_service.update_mmxg(user2);
                request.setAttribute("b", b);
                return "app/grxx/center_mmxg";
            }
        }
    }

    @RequestMapping(value = "center_syzcmx")
    public String center_syzcmx(HttpServletRequest request){
        String loginname = (String)request.getSession().getAttribute("user_loginname");
        List<BusUsersAccHistory> list = users_service.searchByPage_his_app(loginname);
        request.setAttribute("list",list);
        return "app/grxx/center_syzcmx";
    }

    @RequestMapping(value = "center_yglcsp")
    public String center_yglcsp(String id,HttpServletRequest request){
        List<TabUsersShops> list = users_service.select_users_shops(id);
        request.setAttribute("list",list);
        return "app/grxx/center_yglcsp";
    }



}
